package com.qyhl.webtv.module_broke.scoop.mine;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.config.HomeThemeColorUtil;
import com.qyhl.webtv.module_broke.R;
import com.qyhl.webtv.module_broke.R2;
import com.qyhl.webtv.module_broke.utils.radiobutton.MediaManager;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by qyhl on 2017/12/13.
 * 我发布的内容
 */

@Route(path = ARouterPathConstant.SCOOP_MINE, extras = ARouterPathConstant.LOGIN_NEEDED)
public class ScoopMineActivity extends BaseActivity {
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.back_btn)
    ImageView backBtn;
    @BindView(R2.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.viewPager)
    ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.broke_fragment_scoop_mine;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        inits();
    }

    @Override
    protected void setListener() {

    }

    private void inits() {
        if (CommonUtils.getInstance().getSite() == 174) {
            mTitle.setText("我的");
        } else {
            mTitle.setText("我的问答");
        }

        backBtn.setOnClickListener(v -> finish());
        //导航栏
        try {
            Class<? extends SlidingTabLayout> aClass = tabLayout.getClass();
            Field f = aClass.getDeclaredField("mTabPadding");
            f.setAccessible(true);
            f.set(tabLayout, StringUtils.dip2px(this, 15));
        } catch (Exception e) {
            e.printStackTrace();
        }
        viewPager.setAdapter(new ViewPageAdapter(getSupportFragmentManager()));
        tabLayout.setViewPager(viewPager);
        tabLayout.setTextSelectColor(HomeThemeColorUtil.getCheckColor());
        tabLayout.setTextUnselectColor(HomeThemeColorUtil.getTabNormalColor());
        tabLayout.getTitleView(0).getPaint().setFakeBoldText(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("我的内容");
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        MediaManager me = MediaManager.getIntance();
        if (me.isPlay()) {
            //停止播放语音
            me.release();
            //停止动画
            me.getTextView().endAnimation();

            //恢复时间
            me.getTextView().setContent(me.getContentBean().getBrokeNews().getAudioDuration());
        }
        GSYVideoManager.releaseAllVideos();
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("我的内容");
        MobclickAgent.onPause(this);
    }

    private class ViewPageAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> fragmentList;

        ViewPageAdapter(FragmentManager fm) {
            super(fm);
            fragmentList = new ArrayList<>(2);
            fragmentList.add(ScoopMineFragment.newIn(0));
            fragmentList.add(ScoopMineFragment.newIn(1));
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return "公开问答";
            } else {
                return "私密问答";
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (GSYVideoManager.backFromWindowFull(this)) {
            return;
        }
    }
}
